<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>mrdForm</title>
    <link rel="stylesheet" type="text/css" href="../css/jquery/jquery-ui-1.8.23.custom.css"
          media="screen"/>
    <link rel="stylesheet" type="text/css" href="../css/jqgrid/ui.jqgrid.css" media="screen"/>
    <jsp:include page="../../head.jsp"/>
    <script type="text/javascript">
        var state = "${state}";
        $(function () {
            var deptId = $("#deptId").val();
            if (deptId == "") {
                deptId = "${deptId}";
            }
            $("#mrdForm").mrdForm({
                                      title: "查询列表",// 表单标题
                                      columns: [{
                                          name: "name",
                                          label: "用户名称"
                                      }],
                                      buttons: {
                                          'custom': [
                                              {id: "addDept", value: "添加用户", click: add},
                                              {id: "modify", value: "编辑用户", click: edit},
                                              {id: "deleteUser", value: "删除用户", click: delUser},
                                          ]
                                      },
                                      grid: {
                                          id: "columnGrid",
                                          url: "company-listUser?deptId=" + deptId,
                                          colNames: ["id", "用户名", "手机号码", "启用模式", "状态", '用户类型'],
                                          colModel: [{
                                              name: 'id',
                                              index: 'id',
                                              hidden: true,
                                              align: "center"
                                          }, {
                                              name: 'name',
                                              index: 'name',
                                              label: "用户名",
                                              width: 70,
                                              align: "center"
                                          }, {
                                              name: 'mobile',
                                              index: 'mobile',
                                              label: "手机号码",
                                              width: 100,
                                              align: "center"
                                          }, {
                                              name: 'isabled',
                                              index: 'isabled',
                                              label: "启用模式",
                                              width: 70,
                                              align: "center",
                                              //字段值的显示格式转换
                                              formatter: function (cellvalue, options, row) {
                                                  if (cellvalue == "1") {
                                                      return "禁用";
                                                  } else if (cellvalue == "0") {
                                                      return "启用";
                                                  }
                                              }
                                          }, {
                                              name: 'state',
                                              index: 'state',
                                              label: "状态",
                                              width: 50,
                                              align: "center",
                                              //字段值的显示格式转换
                                              formatter: function (cellvalue, options, row) {
                                                  if (cellvalue == "1") {
                                                      return "删除";
                                                  } else if (cellvalue == "0") {
                                                      return "可用";
                                                  }
                                              }
                                          }, {
                                              name: 'type',
                                              index: 'type',
                                              label: '用户类型',
                                              hidden: true
                                          }],
                                          title: "用户列表",
                                          sortname: "ctime",
                                          //rowNum:5,
                                          jsonReader: {
                                              //从哪个json对象中取值
                                              root: "users"
                                          },
                                          onSelectRow: function (id) {
                                              //parent.document.getElementById("rightFrame").src="../base/company-mainUser?dept.id="+id;
                                          },
                                          ondblClickRow: function (id) {
                                              location.href =
                                                      "company-manageUser?user.id=" + id
                                                      + "&dept.id="
                                                      + $("#deptId").val();
                                          }
                                      }
                                  });
            $("#mrdForm").mrdForm("resize");
            if (state == "companyManage") {
                $("#addDept").hide();
                $("#deleteUser").hide();
            }
        });
        //添加
        var add = function () {
            location.href = "company-manageUser?deptId=" + $("#deptId").val();
        };
        var del = function () {
        };
        //编辑
        var edit = function () {
            var selected_rowid = $("#columnGrid").jqGrid("getGridParam", "selrow");
            if (!selected_rowid || selected_rowid == "" || selected_rowid == null) {
                alert("请选择需要编辑的用户");
                return;
            }
            var rowData = $("#columnGrid").getRowData(selected_rowid);
            var userId = rowData.id;
            location.href =
                    "company-manageUser?id=" + userId + "&deptId=" + $("#deptId").val();
        };
        //删除
        var delUser = function () {
            var selected_rowid = $("#columnGrid").jqGrid("getGridParam", "selrow");
            if (!selected_rowid || selected_rowid == "" || selected_rowid == null) {
                alert("请选择需要删除的用户");
                return;
            }
            var rowData = $("#columnGrid").getRowData(selected_rowid);
            var userId = rowData.id;
            var type = rowData.type;
            if (type == "1") {
                alert("该用户为管理员用户不能删除!");
            } else {
                $.ajax({
                           url: "company-delUser",
                           dataType: "json",
                           type: "POST",
                           data: "userId=" + userId + "&type=" + type,
                           success: function (data) {
                               var state = $.parseJSON(data.msg).state;
                               var msg = $.parseJSON(data.msg).msg;
                               if (state == 1) {
                                   alert(msg);
                                   location.href =
                                           "company-mainUser?id=" + $("#deptId").val();
                               }
                           }
                       });
            }

        }
    </script>
<body>
<input type="hidden" id="deptId" value='${deptId}'/>
<div id="mrdForm"></div>
</body>
</html>
